package com.lp.learn.learnspringboot.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;
@Component
public class AsyncTask {

    Random random = new Random();

    private static Logger logger = LoggerFactory.getLogger(AsyncTask.class);

    @Async("taskExecutor")
    public Future<String> taskOne() throws InterruptedException {
        logger.info("开始执行任务一");
        int r = random.nextInt(10000);
        Thread.sleep(r);
        logger.info("taskOne执行时间:"+r);
        return new AsyncResult("taskOne执行完毕");
    }
    @Async("taskExecutor")
    public Future<String> taskTwo() throws InterruptedException {
        logger.info("开始执行任务二");
        int r = random.nextInt(10000);
        Thread.sleep(r);
        logger.info("taskTwo执行时间:"+r);
        return new AsyncResult("taskTwo执行完毕");
    }

    @Async
    public Future<String> taskThree() throws InterruptedException {
        logger.info("开始执行任务三");
        int r = random.nextInt(10000);
        Thread.sleep(r);
        logger.info("taskThree执行时间:"+r);
        return new AsyncResult("taskThree执行完毕");
    }
}
